package cn.heyige.backend.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import cn.heyige.backend.exception.ErrorCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class CommResponse<T> implements Serializable {
    private String desc;
    private int code;
    private T data;

    public CommResponse(int code, String desc, T data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

    public CommResponse(ErrorCode errorType) {
        this.code = errorType.getCode();
        this.desc = errorType.getMessage();
    }

    public static <T> CommResponse<T> ok() {
        return new CommResponse<T>(0, "ok", (T) null);
    }

    public static <T> CommResponse<T> ok(T data) {
        return new CommResponse<T>(0, "ok", data);
    }

    public static <T> CommResponse<T> ok(String desc, T data) {
        return new CommResponse<T>(0, desc, data);
    }

    public static <T> CommResponse<T> fail() {
        return new CommResponse<T>(-1, "fail", (T) null);
    }

    public static <T> CommResponse<T> fail(String desc, T data) {
        return new CommResponse<T>(-1, desc, data);
    }

    public static <T> CommResponse<T> fail(int code, String desc) {
        return new CommResponse<T>(code, desc, (T) null);
    }

    public static <T> CommResponse<T> fail(String desc) {
        return new CommResponse<T>(-1, desc, (T) null);
    }

    public static <T> CommResponse<T> fail(T data) {
        return new CommResponse<T>(-1, "fail", data);
    }


    @JsonIgnore
    public boolean isSuccess() {
        return 0 == this.code;
    }

    @JsonIgnore
    public boolean isFail() {
        return !this.isSuccess();
    }
}
